/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.utility.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.mizartools.system.ElementParseException;


public class XMLFile {

	private XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
	private LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
	private LinkedList<String> processingInstructionList = new LinkedList<String>();
	
	public LinkedList<XMLElement> getXMLElementList(){
		return xmlElementList;
	}

	public LinkedList<String> getProcessingInstructionList(){
		return processingInstructionList;
	}
	
	public void setFile(File file) throws ElementParseException {
		synchronized(XMLFile.class) {
			xmlElementList = new LinkedList<XMLElement>();
			processingInstructionList = new LinkedList<String>();
			FileInputStream stream = null;
			String systemId = file.getName();
			int eventType;
			XMLElement currentXMLElement = null;
			boolean thereAreCharacters = false;
			try {
				 
				stream = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				throw new ElementParseException(ElementParseException.FILE_NOT_FOUND, "Error opening the file " + file.toString());
			}
			XMLStreamReader parser;
			try {
				parser = xmlInputFactory.createXMLStreamReader(systemId, stream);
				
				while (parser.hasNext()){
					eventType = parser.next();
					switch (eventType) {
	//						case XMLStreamConstants.START_DOCUMENT:
	//				            break;
					case XMLStreamConstants.START_ELEMENT:
		            	thereAreCharacters = false;
						currentXMLElement = new XMLElement(parser.getLocalName());
						if (parser.getAttributeCount() != 0){
							for(int i = 0; i < parser.getAttributeCount(); i++){
								currentXMLElement.getXMLAttributeList().add(new XMLAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i)));
							}
						}
		            	xmlElementList.add(currentXMLElement);
			            break;
	//						case XMLStreamConstants.ATTRIBUTE:
	//				            break;
	//						case XMLStreamConstants.END_DOCUMENT:
	//				            break;
		            case XMLStreamConstants.END_ELEMENT:
		            	if (thereAreCharacters){
							currentXMLElement = new XMLElement(parser.getLocalName());
							currentXMLElement.setEndElement(true);
			            	xmlElementList.add(currentXMLElement);
		            	} else
		            	{
		            		currentXMLElement.setEmpty(true);
		            	}
						break;
		            case XMLStreamConstants.CHARACTERS:
		            	if (currentXMLElement.getCharacters() != null) {
			            	currentXMLElement.setCharacters(currentXMLElement.getCharacters() + parser.getText());
		            	} else {
			            	currentXMLElement.setCharacters(parser.getText());
		            	}
		            	thereAreCharacters = true;
						break;
	//			            case XMLStreamConstants.CDATA:
	//							break;
	//			            case XMLStreamConstants.COMMENT:
	//							break;
	//			            case XMLStreamConstants.DTD:
	//							break;
	//			            case XMLStreamConstants.ENTITY_DECLARATION:
	//							break;
	//			            case XMLStreamConstants.ENTITY_REFERENCE:
	//							break;
	//			            case XMLStreamConstants.NAMESPACE:
	//							break;
	//			            case XMLStreamConstants.NOTATION_DECLARATION:
	//							break;
		            case XMLStreamConstants.PROCESSING_INSTRUCTION:
		            	if (parser.getPITarget() != null && parser.getPIData() != null)
		            		processingInstructionList.add(parser.getPITarget() + " " + parser.getPIData());
						break;
	//			            case XMLStreamConstants.SPACE:
	//							break;
					}
				}
				parser.close();
			} catch (XMLStreamException e) {
				throw new ElementParseException(ElementParseException.XML_STREAM, "Impossible parsing file " + file.toString());
			}
			try {
				stream.close();
			} catch (IOException e) {
				throw new ElementParseException(ElementParseException.IO, "Error closing the file " + file.toString());
			}
		}
	}	
}
